package com.global.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.global.dao.GridColumnDao;
import com.global.model.DataSourceXColumn;
import com.global.model.Grid;
import com.global.model.GridColumn;

@Repository("gridColumnDao")
public class GridColumnDaoImpl extends GenericDaoHibernate<GridColumn, Long> implements GridColumnDao {

	public GridColumnDaoImpl() {
		super(GridColumn.class);
	}

	/**
	 * {@inheritDoc}
	 */
	public GridColumn saveGridColumn(GridColumn gridColumn) {
		if(gridColumn!=null){
			if(log.isDebugEnabled()){
				log.debug("gridColumn's id: " + gridColumn.getId());
			}
		}
		
		return gridColumn;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public boolean saveBatchGridColumns(List<GridColumn> gridColumnList) {
        if(gridColumnList!=null){
        	getHibernateTemplate().saveOrUpdateAll(gridColumnList);
    		getHibernateTemplate().flush();
        	
        	return true;
        }
        
        return false;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public GridColumn getGridColumn(Grid grid, String label) {
		if(grid!=null && grid.getId()!=null && StringUtils.isNotBlank(label)){
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
	        queryParams.put("gridId", grid.getId());
	        queryParams.put("label", label);
			List<GridColumn> list = this.findByNamedQuery("findGridColumnByGridAndLabel", queryParams);
			if(!list.isEmpty()){
				return list.get(0);
			}
		}

		return null;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public GridColumn getGridColumn(Grid grid, DataSourceXColumn dataSourceXColumn) {
		if(grid!=null && grid.getId()!=null && dataSourceXColumn!=null){
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
	        queryParams.put("gridId", grid.getId());
	        queryParams.put("dataSourceXColumnId", dataSourceXColumn.getId());
			List<GridColumn> list = this.findByNamedQuery("findGridColumnByGridAndDsod", queryParams);
			if(!list.isEmpty()){
				return list.get(0);
			}
		}

		return null;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public List<GridColumn> getAllGridColumnList(Grid grid) {
		if(grid!=null && grid.getId()!=null){
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
	        queryParams.put("gridId", grid.getId());
	        
			return this.findByNamedQuery("findGridColumnsByGrid", queryParams);
		}
		
		return new ArrayList<GridColumn>();
	}

	/**
	 * {@inheritDoc}
	 */
	public List<GridColumn> getDispalyGridColumnList(Grid grid) {
		if(grid!=null && grid.getId()!=null){
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
	        queryParams.put("gridId", grid.getId());
	        
			return this.findByNamedQuery("findDisplayGridColumnsByGrid", queryParams);
		}
		
		return new ArrayList<GridColumn>();
	}
	
	/**
	 * {@inheritDoc}
	 */
	public List<GridColumn> getGridColumnList(DataSourceXColumn dataSourceXColumn){
		
		if(dataSourceXColumn!=null && dataSourceXColumn.getId()!=null){
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
	        queryParams.put("dataSourceXColumnId", dataSourceXColumn.getId());
	        
	        return this.findByNamedQuery("findGridColumnsByDsod", queryParams);
		}
		
		return new ArrayList<GridColumn>();		
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public List<GridColumn> getGridColumnList(List<DataSourceXColumn> dataSourceXColumnList){
		
		if(dataSourceXColumnList!=null){
			String cIdList = "";
			for(DataSourceXColumn d : dataSourceXColumnList){
				cIdList = cIdList + "," + String.valueOf(d.getId());
			}
			if(StringUtils.isNotBlank(cIdList)){
				cIdList = StringUtils.strip(cIdList, ",");
				String hql = "select g from GridColumn g where g.dataSourceXColumn.id in (" + cIdList + ")";
				
				return (List<GridColumn>)getHibernateTemplate().find(hql);
			}
		}
		
		return new ArrayList<GridColumn>();
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void deleteGridColumn(GridColumn gridColumn) {
		if(gridColumn!=null && gridColumn.getId()!=null){
			getHibernateTemplate().delete(gridColumn);
			getHibernateTemplate().flush();
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void deleteGridColumn(Grid grid, String label) {
		GridColumn gridColumn = getGridColumn(grid, label);
		deleteGridColumn(gridColumn);
	}

	/**
	 * {@inheritDoc}
	 */
	public void deleteGridColumn(Grid grid, DataSourceXColumn dataSourceXColumn) {
		GridColumn gridColumn = getGridColumn(grid, dataSourceXColumn);
		deleteGridColumn(gridColumn);
	}
}
